package logic

import (
	"beike/models"
	"context"
	"fmt"
	"gorm.io/gorm"

	"beike/houserpc/houserpc"
	"beike/houserpc/internal/svc"

	"github.com/zeromicro/go-zero/core/logx"
)

type ResUpdateLogic struct {
	ctx    context.Context
	svcCtx *svc.ServiceContext
	logx.Logger
}

func NewResUpdateLogic(ctx context.Context, svcCtx *svc.ServiceContext) *ResUpdateLogic {
	return &ResUpdateLogic{
		ctx:    ctx,
		svcCtx: svcCtx,
		Logger: logx.WithContext(ctx),
	}
}

func (l *ResUpdateLogic) ResUpdate(in *houserpc.ResUpdateRequest) (*houserpc.ResUpdateResponse, error) {
	// todo: add your logic here and delete this line

	begin := models.DB.Debug().Begin()

	res := models.Reservation{
		Model:  gorm.Model{ID: uint(in.ResId)},
		Status: "已取消",
	}
	err := begin.Updates(&res).Error
	if err != nil {
		begin.Rollback()
		return nil, fmt.Errorf("取消失败")
	}

	begin.Commit()

	return &houserpc.ResUpdateResponse{
		ResId: int64(res.ID),
	}, nil
}
